﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Warehouse.UI.WinForm.Properties;

namespace Warehouse.UI.WinForm.Forms
{
    public class Utility
    {
        private static bool DoFocusFisrt(Control control, bool tabStopOnly)
        {
            return (from Control x in control.Controls
                    where (x.CanFocus && x.TabStop)
                    orderby x.TabIndex
                    select x.Focus()).FirstOrDefault();
        }

        public static bool FocusContainer(Control ctl)
        {
            if (ctl != null)
            {
                if (ctl.HasChildren)
                {
                    var isFocused = DoFocusFisrt(ctl, true);
                    if (isFocused) return true;

                    if (ctl.ContainsFocus == false)
                    {
                        return DoFocusFisrt(ctl, false);
                    }
                }
                return ctl.Focus();
            }
            return false;
        }

        /*public static bool IsFill(Control ctl, ErrorProvider ep, ref bool isLastPass, string displayName = "")
        {
            const string notfill = "未填写";
            if (ctl is LabelTextBox)
            {
                if ((ctl as LabelTextBox).TextBoxText == string.Empty)
                {
                    ep.SetError(ctl, (displayName == string.Empty ? (ctl as LabelTextBox).LabelText : displayName) + notfill);
                    if (isLastPass == true)
                    {
                        ctl.Focus();
                        isLastPass = false;
                    }
                }
            }
            else if (ctl is LabelSelectBox)
            {
                if ((ctl as LabelSelectBox).TextBoxText == string.Empty)
                {
                    ep.SetError(ctl, (displayName == string.Empty ? (ctl as LabelSelectBox).LabelText : displayName) + notfill);
                    if (isLastPass == true)
                    {
                        ctl.Focus();
                        isLastPass = false;
                    }
                }
            }
            return isLastPass;
        }*/

        public static void ShowInfo(string text, IWin32Window owner = null)
        {
            MessageBox.Show(owner, text, Resources.MessageCaption_Info, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static void ShowError(string text, IWin32Window owner = null)
        {
            MessageBox.Show(owner, text, Resources.MessageCaption_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static DialogResult ShowChoose(string text, IWin32Window owner = null, string caption = "")
        {
            return MessageBox.Show(owner, text, (caption == string.Empty) ? Resources.MessageCaption_Question : caption,
                                   MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                   MessageBoxDefaultButton.Button2);
        }

        public static DialogResult ShowChoose2(string text, IWin32Window owner = null, string caption = "")
        {
            return MessageBox.Show(owner, text, (caption == string.Empty) ? Resources.MessageCaption_Question : caption,
                                   MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
                                   MessageBoxDefaultButton.Button3);
        }
    }

    /// <summary>
    /// 列表状态
    /// </summary>
    [Flags]
    public enum BrowseState
    {
        Detached = 1,
        CanAddItem = 2,
        CanEditItem = 4,
        CanDeleItem = 8,
        CanViewItem = 16,
        CanExport = 32,
    }

    /// <summary>
    /// 编辑状态
    /// </summary>
    [Flags]
    public enum EditState
    {
        //     The row has been created but is not part of any System.Data.DataRowCollection.
        //     A System.Data.DataRow is in this state immediately after it has been created
        //     and before it is added to a collection, or if it has been removed from a
        //     collection.
        Detached = 1,
        //     The row has not changed since System.Data.DataRow.AcceptChanges() was last
        //     called.
        Unchanged = 2,
        //     The row has been added to a System.Data.DataRowCollection, and System.Data.DataRow.AcceptChanges()
        //     has not been called.
        Added = 4,
        //     The row was deleted using the System.Data.DataRow.Delete() method of the
        //     System.Data.DataRow.
        Deleted = 8,
        //     The row has been modified and System.Data.DataRow.AcceptChanges() has not
        //     been called.
        Modified = 16,

        ReadOnly = 32,
    }
}
